//
//  NDOrderListItem.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/26.
//

import SwiftUI
import Kingfisher

struct NDOrderListItem: View {
    var model: OrderPachist
    
    var body: some View {
        let orderType = OrderType(rawValue: model.sextalwaysibility?.finalous ?? "")
        let logoUrl = URL.buildURL(with: model.cutad ?? "")
        VStack {
            Spacer(minLength: 16)
            
            HStack(alignment: .top) {
                KFImage(logoUrl)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 70, height: 70)
                    .cornerRadius(10)
                
                VStack(alignment: .leading) {
                    
                    Group {
                        let date = (model.sextalwaysibility?.opac ?? "") + ": " + (model.sextalwaysibility?.edgeard ?? "")
                        Text(date)
                            .font(.system(size: 12))
                            .foregroundColor(Color(hex: "#666666"))
                    }
                    
                    Spacer()
                    
                    Text(model.not ?? "")
                        .font(.system(size: 12, weight: .semibold))
                        .foregroundColor(Color(hex: "#271F24"))
                    
                    Spacer()

                    HStack() {
                        Text("Loan amount")
                            .font(.system(size: 12))
                            .foregroundColor(Color(hex: "#999999"))
                        Spacer()
                        Text(model.sextalwaysibility?.ominance ?? "")
                            .font(.system(size: 14, weight: .medium))
                            .foregroundColor(Color(hex: orderType?.describeColorHex ?? "#1677FF"))
                    }
                }
                
                Spacer()
            }
            
            Rectangle()
                .fill(Color(hex: "#F2F3F5"))
                .frame(height: 1)

            if let lysismost = model.sextalwaysibility?.lysismost,
               !lysismost.isEmpty {
                Text(lysismost)
                    .font(.system(size: 12))
                    .foregroundColor(Color(hex: "#F14C4C"))
                    .frame(maxWidth: .infinity, alignment: .leading)
            }
            
            if let answerize = model.sextalwaysibility?.answerize,
                !answerize.isEmpty {
                HStack(spacing: 20) {
                    Spacer()
                    Text(answerize)
                    .padding(.horizontal, 12)
                    .padding(.vertical, 5)
                    .font(.system(size: 14, weight: .medium))
                    .background(Color(hex: orderType?.backgroundColorHex ?? "#FD632B"))
                    .foregroundColor(Color(hex: "#FFFFFF"))
                    .clipShape(Capsule())
                }
                .padding(.top, 8)
            }
            
            Spacer(minLength: 16)
        }
        .padding(.horizontal, 16)
        .background(Color(hex: "#FFFFFF"))
        .cornerRadius(12)
        .shadow(radius: 4)
    }
}

